package fun.yao.common.monitor.info;

import fun.yao.common.Constant;
import fun.yao.common.msg.HashtableMsg;
import fun.yao.common.uitls.LogUtil;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * map 信息
 * @author longjinglai
 *
 */
public class MapInfo implements IMonitorInfo {

    /**map 详细*/
    private List<MapDetail> alMapDetail = new ArrayList<MapDetail>();


    public MapInfo()
    {

    }

    /**
     * 根据htm构造
     * @param htm
     */
    public MapInfo(HashtableMsg htm)
    {
        for(int i=0;i<htm.size();i++)
        {
            Hashtable<String, String> mapMapInfo = htm.get(i);
            if(mapMapInfo!=null)
            {
                MapDetail detail = new MapDetail(mapMapInfo.get("map_name"), Integer.parseInt(mapMapInfo.get("size")));
                this.alMapDetail.add(detail);
            }
        }
    }


    /**
     * 转换为HashtableMsg
     * @return
     */
    public HashtableMsg toHashtableMsg()
    {
        HashtableMsg htm = new HashtableMsg();
        for(int i=0;i<this.getMapDetail().size();i++)
        {
            Hashtable<String, String> mapMapInfo = new Hashtable<String, String>();
            MapDetail detail = this.getMapDetail().get(i);
            mapMapInfo.put("map_name", detail.getMapName());
            mapMapInfo.put("size", detail.getMapSize()+"");
            htm.add(mapMapInfo);
        }
        return htm;
    }

    public List<MapDetail> getMapDetail()
    {
        return this.alMapDetail;
    }


    public String toString() {
        return "MapInfo [alMapDetail=" + alMapDetail + "]";
    }


    /**
     * map详细
     * @author longjinglai
     *
     */
    public class MapDetail
    {
        /**名称*/
        private String mapName = "";

        /**大小*/
        private int mapSize = 0;

        public String getMapName() {
            return mapName;
        }

        public int getMapSize() {
            return mapSize;
        }

        public MapDetail(String mapName, int mapSize)
        {
            this.mapName = mapName;
            this.mapSize = mapSize;
        }

        @Override
        public String toString() {
            return "MapDetail [mapName=" + mapName + ", mapSize=" + mapSize
                    + "]";
        }
    }


    public void print(StringBuilder sbOut) {
        try
        {
            for(int i=0;i<this.getMapDetail().size();i++)
            {
                MapDetail detailInfo = this.getMapDetail().get(i);
                sbOut.append("第" + i + "个Map["+detailInfo.getMapName()+"]使用情况\n");
                sbOut.append("Map名称："+detailInfo.getMapName()+"，Map大小："+detailInfo.getMapSize()+"\n");
            }
        }catch (Exception e) {
            LogUtil.WriteLog(Constant.BADLY_ERROR, e);
        }
    }
}


